package com.cy.ppj.web.admin.mall;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.mall.StoreAddAO;
import com.cy.ppj.model.ao.mall.StoreEditAO;
import com.cy.ppj.model.domian.mall.Store;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.StoreAdminListQuery;
import com.cy.ppj.model.vo.mall.StoreAdminListVO;
import com.cy.ppj.service.mall.StoreService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 后台-门店管理
 * @Author tanyilin
 * @Date 2019/7/2 17:12
 */
@RestController
@RequestMapping("/admin/mall/store")
@Api(tags = "后台-门店管理")
public class AdminStoreController extends BaseController {

    @Autowired
    private StoreService storeService;

    @ApiOperation("门店列表")
    @GetMapping("/list")
    public PageBean<StoreAdminListVO> getStoreList(StoreAdminListQuery query) {

        PageBean<StoreAdminListVO> pageBean = storeService.getStoreAdminList(query);

        return pageBean;
    }

    @ApiOperation("上下架状态")
    @PostMapping("/update/status")
    public Result updateStoreStatus(@RequestParam(value = "storeId") Long storeId, @RequestParam(value = "status") CommonStatus status) {

        Store updateStore = new Store();
        updateStore.setId(storeId);
        updateStore.setStatus(status);
        storeService.updateStore(updateStore);

        return ok("更改门店状态成功");
    }

    @ApiOperation("新增门店")
    @PostMapping("/do/add")
    public Result doAddStore(@Validated StoreAddAO addAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Store insertStore = new Store();
        BeanUtils.copyProperties(addAO, insertStore);
        storeService.addStore(insertStore);

        return ok("新增门店成功");
    }

    @ApiOperation("编辑门店")
    @PostMapping("/do/edit")
    public Result doEditStore(@Validated StoreEditAO editAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Store updateStore = new Store();
        BeanUtils.copyProperties(editAO, updateStore);
        updateStore.setId(editAO.getStoreId());
        storeService.updateStore(updateStore);

        return ok("编辑门店信息成功");
    }


}
